/*
 * @Author: Xavier 1305433186@qq.com
 * @Date: 2024-05-23 17:39:02
 * @LastEditors: Xavier 1305433186@qq.com
 * @LastEditTime: 2024-07-29 17:12:03
 * @FilePath: \KEIL-ARMd:\Users\HXD\Desktop\拓基\面板\JS32T031_SDK\User\modul_demo\resolution_protocol.c
 * @Description:
 *
 * STAYTRUE (c) 2024 by ${Xavier}, All Rights Reserved.
 */
/*
 * @Author: Xavier 1305433186@qq.com
 * @Date: 2024-05-23 17:39:02
 * @LastEditors: Xavier 1305433186@qq.com
 * @LastEditTime: 2024-05-23 17:51:50
 * @FilePath: \Running_machine_bluetooth_ws8100\example\bluetooth\resolution_protocol.c
 * @Description:
 *
 * STAYTRUE (c) 2024 by ${Xavier}, All Rights Reserved.
 */
#include "ftmp_uart.h"
#include "data_split_group.h"
#include "ble_ftms.h"
#include "stdint.h"
#include "string.h"
extern void uart_send(unsigned char *pBuff, int len);
/*
*功能 ：muc 消息应答
res_code   执行结果
Order      指令
*value     数据
value_len  数据长度
*/
static void Mcu_Ack(uint8_t res_code, uint8_t Order, uint8_t *value, uint32_t value_len)
{
    uint8_t str_temp[26], len_temp = 0; // 临时数据存放
    uint8_t send_buffer[30], send_len = 0;
    send_buffer[0] = str_temp[len_temp++] = FRAME_HEADER; /*帧头*/
    str_temp[len_temp++] = res_code;                      /*执行结果*/
    str_temp[len_temp++] = Order;                         /*指令*/
    str_temp[len_temp++] = value_len;                     /*长度*/
    if (value_len != 0)
    {
        memcpy(&str_temp[len_temp], value, value_len); // 复制数据   /*参数*/
        len_temp += value_len;
    }
    uint8_t cs = 0;
    for (char count = 1; count < len_temp; count++)
    {
        cs += str_temp[count];
    }
    str_temp[len_temp++] = cs; /* cs */
    send_len = data_split(send_buffer + 1, str_temp + 1, len_temp - 1);
    send_len++;
    send_buffer[send_len++] = FRAME_TAIL; /*帧尾*/
    uart_send(send_buffer, send_len);
}

/*
McuSendOrder
功能：MCU 写数据至蓝牙(手机)
调试使用
*/
void McuSendOrder(uint8_t Order, uint8_t *value, uint32_t value_len)
{
    uint8_t str_temp[26], len_temp = 0; // 临时数据存放
    uint8_t send_buffer[40], send_len = 0;
    send_buffer[0] = str_temp[len_temp++] = FRAME_HEADER; // 帧头
    str_temp[len_temp++] = ORDER_MCU_WRITE;               /*指令属性*/
    str_temp[len_temp++] = Order;                         /*指令*/
    str_temp[len_temp++] = value_len;                     /*长度*/
    if (value_len != 0)
    {
        memcpy(&str_temp[len_temp], value, value_len); // 复制数据  /*参数*/
        len_temp += value_len;
    }
    uint8_t cs = 0;
    for (char count = 1; count < len_temp; count++)
    {
        cs += str_temp[count];
    }
    str_temp[len_temp++] = cs; /*cs*/
    send_len = data_split(send_buffer + 1, str_temp + 1, len_temp - 1);
    send_len++;
    send_buffer[send_len++] = FRAME_TAIL; /*帧尾*/
    uart_send(send_buffer, send_len);
}
/**
 * 数据转发至蓝牙
 */
void SendUartDataToApp(hw_u8 *data, hw_u8 length, hw_u8 uuidIndex)
{
    McuSendOrder(uuidIndex, data, length);
}
void UartCommand(char *data, int len)
{
    static uint8_t rx_buff[255], rx_buff_i = 0;
    uint8_t response_state = STATE_SUCCEED;
    for (uint32_t temp = 0; temp < len; temp++)
    {
        rx_buff[rx_buff_i] = data[temp];
        if (rx_buff[rx_buff_i] == FRAME_HEADER)
        { // 出现与帧头相同数据，进入进行判断
            rx_buff_i = 0;
            rx_buff[rx_buff_i] = FRAME_HEADER;
        }
        if ((rx_buff[rx_buff_i]) == FRAME_TAIL)
        { // 出现与帧尾相同数据，进入进行判断
            uint8_t data_temp[64];
            uint8_t data_len = data_group(data_temp, rx_buff, rx_buff_i + 1); // 指令总长度
            uint8_t crc = 0;
            for (uint8_t len = 0; len < data_len - 3; len++)
            {
                crc += data_temp[len + 1];
            }
            if (data_temp[data_len - 2] == crc)
            {                                        // 校验成功
                if (data_temp[1] == ORDER_BLE_WRITE) // BLE 写
                {
                    data_len = data_temp[3]; // 获取参数的长度
                    switch (data_temp[2])
                    { /*指令*/
                    case ORDER_BLE_MTU:
                        if (data_len == 2)
                        {
                            SetPDUSize(data_temp[4] + data_temp[5] << 8);
                            // dbg_printf("set mtu %u", data_temp[4]+data_temp[5]<<8);
                        }
                        else
                        {
                            response_state = STATE_LENFTH_EERROR; // 长度错误
                        }
                        break;
                    case ORDER_CONTROL_POINT:
                        // dbg_printf("ORDER_CONTROL_POINT \r\n  ");
                        FitnessMachineControlPointWrite(&data_temp[4], data_len);
                        break;
                    case ORDER_EXTENSION_DATA:
                        // dbg_printf("ORDER_EXTENSION_DATA \r\n  ");
                        fitnessExtensionDataWrite(&data_temp[4], data_len);
                        break;
                    default:
                        break;
                    }
                }
                else
                {
                    response_state = STATE_FUN_EERROR; // 功能码不存在
                }
            }
            else
            {
                // dbg_printf("校验失败 crc=%x data_temp[crc] =%d", crc, data_temp[data_len - 2]);
                response_state = STATE_CRC_FAILURE;
            }
            if (response_state != STATE_SUCCEED)
                Mcu_Ack(response_state, data_temp[2], NULL, 0); //
        }
        if (rx_buff_i++ >= sizeof(rx_buff))
        {
            rx_buff_i = 0; // 限制最大读取值，防止指针越界
        }
    }
}
